<!--
 * @Description:缓动动画原理
 * @Author: Sun
 * @Date: 2019-07-01 20:05:50
 * @LastEditors: Sun
 * @LastEditTime: 2019-07-24 21:50:11
 -->
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>缓动动画原理</title>
	<style>
		* {
			margin: 0;
			padding: 0
		}

		div{
			width: 100px;
			height: 100px;
			background-color:pink;
			position: absolute;
			left: 0px;
		}
	</style>
</head>

<body>
 <button id="btn">开始</button>
 <div id="box"></div>		

	<script>
		function $(id) {
			return document.getElementById(id);
		}

		function show(obj) {
			return obj.style.display = "block"
		}

		function hide(obj) {
			return obj.style.display = "none"
		}

		function scroll() {
			if (window.pageYOffset != null) { //ie9+
				return {
					left: window.pageXOffset,
					top: window.pageYOffset
				}
			} else if (document.compatMode == "CSS1Compat") { //不是怪异模式浏览器 <!DOCTYPE html>
				return {
					left: document.documentElement.scrollLeft,
					top: document.documentElement.scrollTop
				}
			} else { //怪异模式浏览器
				return {
					left: document.body.scrollLeft,
					top: document.body.scrollTop
				}
			}
		}

		window.onload = function () {
			var btn = $("btn")
			var box = $("box")
			var target=400;
			var timer=null;
		  btn.onclick=function(){//盒子本身位置+步长(不断变化的)Q
			  timer=setInterval(()=>{
					var step=(target-box.offsetLeft)/10
					// step=Math.ceil(step) //0-400 ⬆上
			  	//	step=Math.floor(step)//400-0 下
					step=step>0?Math.ceil(step):Math.floor(step)
					box.style.left=box.offsetLeft+step+"px";
					if(box.offsetLeft==target){
						clearInterval(timer)
						console.log("0000")
					}
				},30)
			}

		}
	</script>

</body>

</html>